﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Code.Models;
using Code.BLL;
using Code.Commen;

namespace QianBuo.SchemeTool.Forms
{
    public partial class SchemeDetailForm : Form
    {
        public SchemeDetailForm(SchemeManage.SchemeItemModel scheme)
        {
            InitializeComponent();
            Scheme = scheme;
            SchemeID = scheme.SchemeID;
            dgvWinDetail.AutoGenerateColumns = false;
            dgvSchemeDetail.AutoGenerateColumns = false;
            dgvSchemeDetail.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
            dgvWinDetail.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
        }

        public Guid SchemeID { get; set; }
        private SchemeManage.SchemeItemModel Scheme { get; set; }

        private void SchemeDetailForm_Load(object sender, EventArgs e)
        {
            InitControl();
            BindData();
        }

        private void BindData()
        {
            var SM =new SchemeManage();
            if (Scheme == null)
                Scheme = SM.GetSchemeDetail(null, null).FirstOrDefault(p => p.SchemeID == SchemeID);
            if (Scheme == null)
            {
                MessageBox.Show("不存在该方案！");
                this.Close();
                return;
            }

            txtUserName.Text = Scheme.UserName;
            txtAddTime.Text = Scheme.AddTime.ToString("yyyy-MM-dd HH:mm:ss");
            txtIssueName.Text = Scheme.IssueName;
            txtMultiple.Text = Scheme.Multiple.ToString();
            txtOpenCode.Text = Scheme.OpenCode;
            txtSchemeCount.Text = Scheme.SchemeCount.ToString();
            txtSchemeMoney.Text = Scheme.SchemeMoney.ToString("F2");
            txtWinMoney.Text = string.Format("{0:F2}", Scheme.WinMoney);

            txtLotteryName.Text = Scheme.LotteryName;
            txtIsChaseTask.Text = Scheme.IsChaseTask ? "是" : "否";
            txtStateName.Text = Scheme.StateName;

            txtRatioName.Text = Scheme.RatioName;
            txtWinModelName.Text = Scheme.WinModelName;

            var pls = SM.GetSchemePlays(SchemeID).ToArray();
            var wis = SM.GetSchemeWinMoneys(SchemeID).ToArray();
            wis.Each(p => { if (p.UserID == Scheme.UserID) p.UserName += "(方案发起人)"; });

            dgvSchemeDetail.DataSource = pls;
            dgvWinDetail.DataSource = wis;
            split.Panel1Collapsed = wis.Length == 0;
        }

        private void InitControl()
        {

        }

        private void menuCpoy_Click(object sender, EventArgs e)
        {
            DataGridView dgv=null;
            if (dgvWinDetail.Focused)
                dgv = dgvWinDetail;
            else if (dgvSchemeDetail.Focused)
                dgv = dgvSchemeDetail;

            if (dgv != null && dgv.GetCellCount(DataGridViewElementStates.Selected) > 0)
            {
                try
                {
                    // Add the selection to the clipboard.
                    Clipboard.SetDataObject(dgv.GetClipboardContent());
                }
                catch (System.Runtime.InteropServices.ExternalException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnRefsh_Click(object sender, EventArgs e)
        {
            Scheme = null;
            BindData();
        }

        private void dgvSchemeDetail_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var cell = dgvSchemeDetail.CurrentCell;
            if (cell != null && cell.OwningColumn.DataPropertyName == "Number")
            {

                var item = cell.OwningRow.DataBoundItem as SchemeManage.SchemePlayModel;
                txtNumber.Text = item.Number;
                txtNumber.Tag = item;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
       {
            var item=txtNumber.Tag as SchemeManage.SchemePlayModel;
            if(item==null)
                return;
            if (item.Number == txtNumber.Text)
                return;

            var f = Code.BLL.Arithmetic.LotteryFactory.CreateLottery((LotteryType)Scheme.LotteryID);
            var pl = f.GetPlayType((PlayType_E)CacheManage.PlayType[item.PlayTypeID].PlayTypeListID);
            var number = pl.RegistNum(txtNumber.Text);
            if (number == null)
            {
                MessageBox.Show("号码格式不正确！");
                return;
            }
            if (pl.GetNumCount(txtNumber.Text) != item.Count)
            { 
                MessageBox.Show("注数不一至！");
                return;
            }

            var SM = new SchemeManage();
            int rs = SM.Update<Scheme_Detail>(new { ChoseNumber = txtNumber.Text }, new { ID = item.ID });
            if (rs > 0)
            {
                MessageBox.Show("保存成功！");
                Scheme = null;
                BindData();
            }
            else
            {
                MessageBox.Show("保存失败!");
            }
        }
    }
}
